//
//  PersonDataVC.m
//  MZJ
//
//  Created by XDD on 16/6/3.
//  Copyright © 2016年 XDD. All rights reserved.
//

#import "PersonDataVC.h"
#import "PersonDataCell.h"
#import "BaseDataVC.h"
#import "MyPublicVC.h"
#import "MyhelpVC.h"
#import "MyActiveVC.h"
#import "EditDataVC.h"
#import "SettingViewController.h"
#import "EditDataController.h"
#import "AnswerViewController.h"
#import "SystemWarningVC.h"

@interface PersonDataVC () <UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
    NSArray *nameArr;
    NSArray *nameArr_img;
    UIImage *_faceImage;
    NSString *choose_img_updt_Str;   //上传头像或是背景判断
}

@property (nonatomic, strong) UITableView *personData_TabelView;
@property (nonatomic, strong) NetWorkingSModel *model;
@property (nonatomic, strong) ModelViewController *modelViewController;
@property (nonatomic, strong) UILabel *loveLabel;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UILabel *activeLabel;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *headImageView;


@end

@implementation PersonDataVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
//    self.tabBarController.tabBar.hidden = YES;
    
    [self person_Data_Request];
}

- (void) person_Data_Request {
    if ([[DataModel object].statu_Str intValue] == 0) {
        __weak typeof (self)weakSelf = self;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *token = [user objectForKey:@"user_token"];
        
        if (token.length != 0) {
            
            [dic setObject:token forKey:@"token"];
        }
        
        [_model setBlockWithReturnBlock:^(id returnValue) {
            
            NSLog(@"个人信息 = %@",returnValue);
            [DataModel object].userDetail = returnValue[@"result"];
            [weakSelf.modelViewController userInfo_data_save:@"useDetail.txt" responseObject:returnValue[@"result"]];
            //        NSLog(@"%@",[NSString stringWithFormat:@"%.2f 元",[returnValue[@"result"][@"uhelp_money"] floatValue]/100]);
            [weakSelf.personData_TabelView reloadData];
        } WithErrorBlock:^(id errorCode) {
            [DataModel object].statu_Str = errorCode[@"statu"];
//            [weakSelf waringAction:errorCode[@"result"]];
        }];
        [weakSelf.model userGetUserinfo:dic];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.topBar.backgroundColor = [UIColor colorWithRed:253/255.0 green:179/255.0 blue:10/255.0 alpha:1];
//    self.titleLabel.text = @"个人中心";
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
//    [self.backButton setTitle:@"" forState:UIControlStateNormal];
    
    if (!_modelViewController) {
        _modelViewController = [[ModelViewController alloc] init];
        if (!_model) {
            _model = [[NetWorkingSModel alloc] init];
            //            [self person_data_httprequest];
        }
    }
    nameArr = @[@"基本资料",@"我的求助",@"我的救助",@"我的志愿者活动",@"我的回复",@"系统提醒",@"设置"];
    nameArr_img = @[@"base_data_img@2x",@"wodeqiuzhu@2mgx",@"wodejiuzhu_img@2x",@"wodezhiyuanzhe_img@2x",@"wodehuifu_img@2x",@"xitongtixing_img@2x",@"setting_img@2x"];

//    nameArr1 = @[@"系统提醒",@"设置"];
    
    self.personData_TabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 50)];
    self.personData_TabelView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    self.personData_TabelView.delegate = self;
    self.personData_TabelView.dataSource = self;
    [self.personData_TabelView registerNib:[UINib nibWithNibName:@"PersonDataCell" bundle:nil] forCellReuseIdentifier:@"personDataCell"];
    self.personData_TabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.personData_TabelView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

//点击返回键
//- (void)clickedBackButton:(UIButton *)sender {
//
//    [self.navigationController popViewControllerAnimated:YES];
//    self.tabBarController.tabBar.hidden = NO;
//}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
//        if (indexPath.row == 0) {
        return 0;
//        }
    }
    if (indexPath.section == 1) {
        return 46;
    }
    return 46;
}

//Header的高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 200;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *head = [[UIView alloc]init];
//    head.backgroundColor = [UIColor blackColor];
    if (section == 0) {
        head.frame = CGRectMake(0, 0, kWidth, 200);
        head.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
        
        _backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 200)];
        _backgroundImageView.backgroundColor = [UIColor colorWithRed:255/255.0 green:182/255.0 blue:0/255.0 alpha:1];
        if (IsNilString([DataModel object].userDetail[@"ubgimg"])) {
            
        } else {
            [_backgroundImageView sd_setImageWithURL:[NSURL URLWithString:[DataModel object].userDetail[@"ubgimg"]]
                                    placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        }
        
        [head addSubview:_backgroundImageView];
        
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth/2 - 30, 50, 60, 60)];
        _headImageView.backgroundColor = [UIColor purpleColor];
        _headImageView.layer.cornerRadius = 30.0;
        if (IsNilString([DataModel object].userDetail[@"uheadimg"])) {
            _headImageView.image = [UIImage imageNamed:@"80px"];
        } else {
            [_headImageView sd_setImageWithURL:[NSURL URLWithString:[DataModel object].userDetail[@"uheadimg"]]
                              placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        }
        
        _headImageView.layer.masksToBounds = YES;
        [head addSubview:_headImageView];
        
        UIButton *exchange_imageView_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        exchange_imageView_Btn.frame = CGRectMake(kWidth/2 - 30, 50, 60, 60);
        exchange_imageView_Btn.backgroundColor = [UIColor clearColor];
        [exchange_imageView_Btn addTarget:self action:@selector(exchange_imageView_BtnAction) forControlEvents:UIControlEventTouchUpInside];
        [head addSubview:exchange_imageView_Btn];
        
        UIButton *editPerson_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        editPerson_Btn.backgroundColor = [UIColor redColor];
        [editPerson_Btn setImage:[UIImage imageNamed:@"xiugai_img@2x"] forState:UIControlStateNormal];
        [editPerson_Btn addTarget:self action:@selector(editPerson_BtnAction) forControlEvents:UIControlEventTouchUpInside];
        editPerson_Btn.frame = CGRectMake(kWidth - 50, 60, 40, 40);
        [head addSubview:editPerson_Btn];
        
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 125, kWidth - 20, 20)];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:15];
        nameLabel.textColor = [UIColor whiteColor];
        if (IsNilString([DataModel object].userDetail[@"unickname"])) {
            nameLabel.textColor = [UIColor whiteColor];
            nameLabel.text = @"请登录";
        } else {
            nameLabel.text = [DataModel object].userDetail[@"unickname"];

        }
        [head addSubview:nameLabel];
        
        UIView *pagesView = [[UIView alloc]initWithFrame:CGRectMake(0, 160, kWidth, 40)];
        pagesView.backgroundColor = [UIColor blackColor];
        pagesView.alpha = 0.4;
        [head addSubview:pagesView];
        
        UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0.3*kWidth, 7, 1, 26)];
        line1.backgroundColor = [UIColor whiteColor];
        [pagesView addSubview:line1];
        
        UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0.7*kWidth, 7, 1, 26)];
        line2.backgroundColor = [UIColor whiteColor];
        [pagesView addSubview:line2];
        
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(1, 3, 0.3*kWidth - 2, 18)];
        label1.text = @"爱心捐赠(次)";
        label1.backgroundColor = [UIColor blackColor];
        label1.textColor = [UIColor whiteColor];
        label1.textAlignment = NSTextAlignmentCenter;
        label1.font = [UIFont systemFontOfSize:12];
        [pagesView addSubview:label1];
        
        _loveLabel = [[UILabel alloc]initWithFrame:CGRectMake(1, 21, 0.3*kWidth - 2, 18)];
        _loveLabel.text = @"0 次";
        self.loveLabel.text = [NSString stringWithFormat:@"%d 次",[[DataModel object].userDetail[@"help_count"] intValue]];
        _loveLabel.textColor = [UIColor whiteColor];
        _loveLabel.textAlignment = NSTextAlignmentCenter;
        _loveLabel.font = [UIFont systemFontOfSize:12];
        [pagesView addSubview:_loveLabel];
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0.3*kWidth - 1, 3, 0.4*kWidth - 2, 18)];
        label2.text = @"累积金额(元)";
        label2.textColor = [UIColor whiteColor];
        label2.textAlignment = NSTextAlignmentCenter;
        label2.font = [UIFont systemFontOfSize:12];
        [pagesView addSubview:label2];
        
        _moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.3*kWidth - 1, 21, 0.4*kWidth - 2, 18)];
        _moneyLabel.text = @"0.00元";
        _moneyLabel.textColor = [UIColor whiteColor];
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        self.moneyLabel.text = [NSString stringWithFormat:@"%.2f 元",[[DataModel object].userDetail[@"uhelp_money"] floatValue]/100];
        _moneyLabel.font = [UIFont systemFontOfSize:12];
        [pagesView addSubview:_moneyLabel];
        
        UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0.7*kWidth - 1, 3, 0.3*kWidth - 2, 18)];
        label3.text = @"志愿者活动(次)";
        label3.textColor = [UIColor whiteColor];
        label3.textAlignment = NSTextAlignmentCenter;
        label3.font = [UIFont systemFontOfSize:12];
        [pagesView addSubview:label3];
        
        _activeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.7*kWidth - 1, 21, 0.3*kWidth - 2, 18)];
        _activeLabel.text = @"12次";
        self.activeLabel.text = [NSString stringWithFormat:@"%d 次",[[DataModel object].userDetail[@"ulevel"] intValue]];
        _activeLabel.textColor = [UIColor whiteColor];
        _activeLabel.textAlignment = NSTextAlignmentCenter;
        _activeLabel.font = [UIFont systemFontOfSize:12];
        [pagesView addSubview:_activeLabel];

        
        return head;
    }
//    if (section == 1) {
//        head.frame = XJFlexibleFrame(CGRectMake(0, 0, 320, 7), NO);
//        head.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
//        return head;
//    }
//    if (section == 2) {
//        head.frame = XJFlexibleFrame(CGRectMake(0, 0, 320, 7), NO);
//        head.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
//        return head;
//    }
    return 0;
}

//跳转个人编辑资料
- (void)editPerson_BtnAction {
    if ([[DataModel object].statu_Str intValue] == 0) {
        EditDataController *editDataVC = [[EditDataController alloc]init];
        [self.navigationController pushViewController:editDataVC animated:YES];
    } else {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }

}

//选择换背景或是头像
- (void)exchange_imageView_BtnAction {
    if ([[DataModel object].statu_Str intValue] == 0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"修改头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            choose_img_updt_Str = @"headImg";
            UIAlertController *head_alertController = [UIAlertController alertControllerWithTitle:@"修改头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *head_action1 = [UIAlertAction actionWithTitle:@"本地相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //用户相册
                [self takePhotoPicker:UIImagePickerControllerSourceTypePhotoLibrary];
            }];
            UIAlertAction *head_action2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //拍照
                [self takePhotoPicker:UIImagePickerControllerSourceTypeCamera];
            }];
            UIAlertAction *head_action3 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [head_alertController addAction:head_action1];
            [head_alertController addAction:head_action2];
            [head_alertController addAction:head_action3];
            [self presentViewController:head_alertController animated:YES completion:nil];
            
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"修改封面" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            choose_img_updt_Str = @"backgroundImg";
            UIAlertController *background_alertController = [UIAlertController alertControllerWithTitle:@"修改封面" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *background_action1 = [UIAlertAction actionWithTitle:@"本地相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //用户相册
                [self takePhotoPicker:UIImagePickerControllerSourceTypePhotoLibrary];
            }];
            UIAlertAction *background_action2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //拍照
                [self takePhotoPicker:UIImagePickerControllerSourceTypeCamera];
            }];
            UIAlertAction *background_action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [background_alertController addAction:background_action1];
            [background_alertController addAction:background_action2];
            [background_alertController addAction:background_action3];
            [self presentViewController:background_alertController animated:YES completion:nil];
        }];
        
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertController addAction:action1];
        [alertController addAction:action2];
        [alertController addAction:action3];
        [self presentViewController:alertController animated:YES completion:nil];

    } else {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    if (section == 1) {
        return nameArr.count;
    }
//    if (section == 2) {
//        return 2;
//    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
//    NSInteger row = indexPath.row;
    
    static NSString *identifier = @"personDataCell";
    PersonDataCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
 
    if (!cell) {
        
        cell = [[PersonDataCell alloc] initWithFrame:CGRectZero];
    }

    cell.pointView.hidden = YES;
    if (section == 0) {
//        cell.classImageView.hidden = YES;
//        cell.className_Label.hidden = YES;
//        cell.lineView.hidden = YES;
//        cell.nameLabel.text = [DataModel object].userDetail[@"unickname"];
//        cell.starNumLabel.text = [NSString stringWithFormat:@"%@星",[DataModel object].userDetail[@"ulevel"]];
//        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[DataModel object].userDetail[@"uheadimg"]]
//                              placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    }
    if (section == 1) {
        cell.className_Label.text = nameArr[indexPath.row];
        cell.classImageView.image = [UIImage imageNamed:nameArr_img[indexPath.row]];
        cell.headImageView.hidden = YES;
        cell.starNumLabel.hidden = YES;
        cell.starImageView.hidden = YES;
        cell.nameLabel.hidden = YES;
    }
//    if (section == 2) {
//        cell.className_Label.text = nameArr1[indexPath.row];
//        cell.headImageView.hidden = YES;
//        cell.starNumLabel.hidden = YES;
//        cell.starImageView.hidden = YES;
//        cell.nameLabel.hidden = YES;
//        if (row == 1) {
//            cell.lineView.hidden = YES;
//        }
//        if (row == 0) {
//            cell.pointView.hidden = NO;
//        }
//
//    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            if ([[DataModel object].statu_Str intValue] == 0) {
                BaseDataVC *baseDataVC = [[BaseDataVC alloc]init];
                [self.navigationController pushViewController:baseDataVC animated:YES];
            } else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self.navigationController pushViewController:loginVC animated:YES];
            }
        }
        if (indexPath.row == 1) {
            if ([[DataModel object].statu_Str intValue] == 0) {
                MyPublicVC *myPublicVC = [[MyPublicVC alloc]init];
                [self.navigationController pushViewController:myPublicVC animated:YES];
            } else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self.navigationController pushViewController:loginVC animated:YES];
            }

        }
        if (indexPath.row == 2) {
            if ([[DataModel object].statu_Str intValue] == 0) {
                MyhelpVC *myhelpVC = [[MyhelpVC alloc]init];
                [self.navigationController pushViewController:myhelpVC animated:YES];
            } else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self.navigationController pushViewController:loginVC animated:YES];
            }

        }
        if (indexPath.row == 3) {
            if ([[DataModel object].statu_Str intValue] == 0) {
                MyActiveVC *myActiveVC = [[MyActiveVC alloc]init];
                [self.navigationController pushViewController:myActiveVC animated:YES];
            } else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self.navigationController pushViewController:loginVC animated:YES];
            }

        }
        if (indexPath.row == 4) {
            if ([[DataModel object].statu_Str intValue] == 0) {
                AnswerViewController *answerViewController = [[AnswerViewController alloc]init];
                [self.navigationController pushViewController:answerViewController animated:YES];
            } else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self.navigationController pushViewController:loginVC animated:YES];
            }
        }
        if (indexPath.row == 5) {
            if ([[DataModel object].statu_Str intValue] == 0) {

                SystemWarningVC *systemWarningVC = [[SystemWarningVC alloc]init];
                [self.navigationController pushViewController:systemWarningVC animated:YES];
            } else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self.navigationController pushViewController:loginVC animated:YES];
            }
        }
        if (indexPath.row == 6) {
            SettingViewController *settingViewController = [[SettingViewController alloc]init];
            [self.navigationController pushViewController:settingViewController animated:YES];
        }
    }
//    if (indexPath.section == 0) {
//        EditDataVC *editDataVC = [[EditDataVC alloc]init];
//        [self.navigationController pushViewController:editDataVC animated:YES];
//    }
//    if (indexPath.section == 2) {
//        if (indexPath.row == 1) {
//            SettingViewController *settingViewController = [[SettingViewController alloc]init];
//            [self.navigationController pushViewController:settingViewController animated:YES];
//        }
//    }
}

- (void)takePhotoPicker:(UIImagePickerControllerSourceType)sourceType {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
    picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:picker animated:YES completion:nil];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *image= [info objectForKey:@"UIImagePickerControllerEditedImage"];
    _faceImage = image;
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        //如果是拍照，保存到相册
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    
    //上传头像
    NSData *formData = UIImagePNGRepresentation(image);
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *token = [user objectForKey:@"user_token"];
    [dic setObject:token forKey:@"token"];
    
//    choose_img_updt_Str = @"backgroundImg";
    
    if ([choose_img_updt_Str isEqualToString:@"backgroundImg"]) {
        [NetRequestClass NetUploadFilesPOSTWithRequestURL:bgimg withParameter:dic data:formData compressData:imageData name:@"bgimg" fileName:@"file.png" mimeType:@"image/png" WithReturnValeuBlock:^(id returnValue) {
            
            NSLog(@"");
            //        [SVProgressHUD showInfoStatus:@"头像上传成功" afterDelay:1.0];
            
            NSData *data =[returnValue dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary* dict_ = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if ([dict_[@"statu"] intValue] == 0) {
                self.backgroundImageView.image = _faceImage;
            } else {
                [self waringAction:dict_[@"result"]];
            }
            NSLog(@"上传封面 = %@",returnValue);
        } WithErrorCodeBlock:^(id errorCode) {
            
            NSLog(@"上传失败");
            //        [SVProgressHUD showInfoStatus:@"头像上传失败" afterDelay:1.0];
            
        } WithFailureBlock:^{
            
            NSLog(@"上传失败");
            //        [SVProgressHUD showInfoStatus:@"头像上传失败" afterDelay:1.0];
        }];
    }
    else if ([choose_img_updt_Str isEqualToString:@"headImg"]) {
        [NetRequestClass NetUploadFilesPOSTWithRequestURL:headimg withParameter:dic data:formData compressData:imageData name:@"headeimg" fileName:@"file.png" mimeType:@"image/png" WithReturnValeuBlock:^(id returnValue) {
            
            NSData *data =[returnValue dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary* dict_ = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if ([dict_[@"statu"] intValue] == 0) {
                self.headImageView.image = _faceImage;
            } else {
                [self waringAction:dict_[@"result"]];
            }
            //        [SVProgressHUD showInfoStatus:@"头像上传成功" afterDelay:1.0];
            
            [self person_Data_Request];
            
            NSLog(@"头像上传 = %@",returnValue);
            
        } WithErrorCodeBlock:^(id errorCode) {
            
            NSLog(@"上传失败");
            //        [SVProgressHUD showInfoStatus:@"头像上传失败" afterDelay:1.0];
            
        } WithFailureBlock:^{
            
            NSLog(@"上传失败");
            //        [SVProgressHUD showInfoStatus:@"头像上传失败" afterDelay:1.0];
        }];
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//图片压缩到指定大小
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
    UIImage *sourceImage = self.headImageView.image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
